package com.demo.delivery.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.GridView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.demo.delivery.R;
import com.demo.delivery.activity.FoodActivity;
import com.demo.delivery.adapter.HomeTitleAdapter;
import com.demo.delivery.adapter.StoreAdapter;
import com.demo.delivery.data.DataContent;
import com.demo.delivery.entity.Store;
import com.demo.delivery.utils.ImmersionBarUtils;

public class HomeFragment extends Fragment {

    private GridView gridView;

    private RecyclerView recyclerView;

    private EditText etSearch;

    private StoreAdapter storeAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ImmersionBarUtils.immersion(this, true);
        bindView();
        initData();
        initView();
    }

    private void bindView() {
        gridView = getView().findViewById(R.id.gv);
        recyclerView = getView().findViewById(R.id.rv);
        etSearch = getView().findViewById(R.id.et_search);
    }

    private void initData() {
        storeAdapter = new StoreAdapter();
        storeAdapter.setOnItemClickListener(new StoreAdapter.OnItemClickListener() {
            @Override
            public void onClick(Store item) {
                Intent intent = new Intent(getContext(), FoodActivity.class);
                intent.putExtra("store", item);
                startActivity(intent);
            }
        });
    }

    private void initView() {
        gridView.setAdapter(new HomeTitleAdapter());
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(storeAdapter);
        storeAdapter.setList(DataContent.getStoreList(null));
        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                storeAdapter.setList(DataContent.getStoreList(s.toString()));
            }
        });
    }
}
